function longestCommonPrefix(str) {
    if(!str.length) return '';
    let pre = str[0];
    for(let i = 1; i< str.length; i++){
        while(str[i].indexOf(pre) !== 0){
            pre = pre.slice(0, pre.length - 1);
            if(!pre) return '';
        }
    }
    return pre;
}

console.log(longestCommonPrefix(["flower", "flow", "flight"]));
